use std::{
    io::Read,
    process::exit,
    slice,
};

enum State {
    Init,
    Zero,
    NonZero(u64),
}

fn main() {
    fn advance_or_end(refm_br: &mut impl Read, refm_b: &mut u8) {
        match refm_br.read(slice::from_mut(refm_b)) {
            Ok(0) => exit(1),
            Err(e) => {
                eprintln!("IO Error: {e:?}");
                exit(-1)
            }
            _ => {}
        }
    }

    fn produce(val: u64) {
        print!("{val}");
        exit(0)
    }

    // it turns out that `io::stdin()` construsts a `BufReader`
    let mut br = std::io::stdin();
    let mut state = State::Init;
    let mut c = 0_u8;

    advance_or_end(&mut br, &mut c);

    loop {
        match state {
            State::Init => {
                match c {
                    b'0' => state = State::Zero,
                    it @ b'1'..=b'9' => state = State::NonZero((it - b'0') as u64),
                    _ => {}
                }
                advance_or_end(&mut br, &mut c);
            }
            State::Zero => match c {
                b'%' => produce(0),
                _ => state = State::Init,
            },
            State::NonZero(ref mut refm_val) => match c {
                b'%' => produce(*refm_val),
                it @ b'0'..=b'9' => {
                    *refm_val = *refm_val * 10 + (it - b'0') as u64;
                    advance_or_end(&mut br, &mut c)
                }
                _ => {
                    state = State::Init;
                    advance_or_end(&mut br, &mut c)
                }
            },
        }
    }
}
